import { convertToChineseNumber } from "@/utils/commonHooks";
import { getPlantsApi, getYearsApi, getWorkShopsApi } from "@/api/plant/index";
import {
  getWeeksInYear,
  getWeekNumber,
  week_date,
  getDaysOfCurrentWeek,
} from "@/utils/dateUtils";
import type { TableColumnCtx } from "element-plus";

export interface WeekDetailType {
  week: number;
  date: string;
}

export interface SpanMethodProps {
  row: any;
  column: any;
  rowIndex: number;
  columnIndex: number;
}

export interface SearchType {
  planWeek: string | number;
  planYear: string | number;
  workshopCode?: string;
  workCenterCode?: string;
  workCenterId?: string;
  plantId: string;
}

export interface SummaryMethodProps<T = any> {
  columns: TableColumnCtx<T>[];
  data: T[];
}

export function ToChineseDate(a: any) {
  return convertToChineseNumber(a, 2) === "七"
    ? "日"
    : convertToChineseNumber(a, 2);
}

// 获工厂列表
export function getPlantList() {
  return new Promise(async (resolve, reject) => {
    const { data } = await getPlantsApi();
    resolve(data);
  });
}

// 获取年列表
export async function getYearList() {
  return new Promise(async (resolve, reject) => {
    const { data } = await getYearsApi();
    resolve(data);
  });
}

// 获取周列表
export function getMonthList(year: number) {
  const weekCount = getWeeksInYear(year);
  const weekList = [];
  for (let i = 1; i <= weekCount; i++) {
    weekList.push({
      value: i,
      label: `第${i}周`,
    });
  }
  return weekList;
}

// 根据id获取列表中的名称
export function getNameInfo(list: any, id: string, name: string) {
  const arr = list.filter((e: any) => e.id === id);
  return arr[0] ? arr[0][name] : "";
}

const currentYear = new Date().getFullYear(); // 当前年份
const currentWeek = getWeekNumber(new Date()); // 当前第几周

// 计算新建行内容
export function getOneLineDate() {
  const oneLine = JSON.parse(JSON.stringify(oneTable[0]));
  oneLine.planYear = currentYear;
  oneLine.planWeek = currentWeek;
  return oneLine;
}

// 前端唯一值计算
export function generateUniqueKey() {
  const key = "key_";
  const time = Date.now().toString(36);
  const random = Math.random().toString(36).substr(2, 9);
  return key + time + random;
}

// 处理接口保存数据
export function resolveSaveData() {
  const data: any = [];
  return data;
}

export function filterRequestIds(data: any) {
  let ids = "";
  if (data && data?.length) {
    data?.map((e: any) => {
      ids += `${e.id},`;
    });
    ids = ids.slice(0, -1);
  }
  return ids;
}
export const mockData = [
  {
    id: "1",
    createTime: null,
    lastUpdateTime: null,
    createdUserId: null,
    lastUpdatedUserId: null,
    createdUserName: null,
    lastUpdatedUserName: null,
    producePlanCode: null,
    organizationId: null,
    organizationCode: null,
    plantId: "4d79fc29b3ef4c1eaace6bedb5e71a2e",
    plantCode: null,
    workshopId: null,
    workshopCode: null,
    workshopSectionId: null,
    workshopSectionCode: null,
    workCenterId: "1784863304673964034",
    workCenterCode: null,
    planBeginDate: null,
    planEndDate: null,
    planYear: 2024,
    planWeek: 17,
    materialId: "1111",
    materialCode: "1111code",
    planState: 2,
    planQuantity: 1212.0,
    planBatch: "202410",
    planFinishQuantity: null,
    quantityUnit: null,
    externalCode: null,
    version: null,
    remarks: null,
    deleted: 0,
    ids: null,
    yield: "3000",
    routingAllocationId: "1",
    orderList: [
      {
        id: "1",
        createTime: "2024-05-06 11:03:36",
        lastUpdateTime: null,
        createdUserId: null,
        lastUpdatedUserId: null,
        createdUserName: null,
        lastUpdatedUserName: null,
        produceOrderCode: "1",
        produceOrderType: 1,
        organizationId: "1",
        organizationCode: "100",
        plantId: "1737270157287362562",
        plantCode: "1000",
        workshopId: "1",
        workshopCode: "1",
        workshopSectionId: "1",
        workshopSectionCode: "1",
        workCenterId: "1",
        workCenterCode: "1",
        planDate: "2024-04-23 11:03:17",
        shiftSystemId: "1",
        shiftId: "1",
        routingAllocationId: "1",
        materialId: "1",
        materialCode: "1",
        planQuantity: 1.0,
        planBatch: "1",
        planFinishQuantity: 2.0,
        quantityUnit: "1",
        produceOrderSource: "1",
        producePlanId: "1",
        producePlanCode: "1",
        parentProduceOrderId: "1",
        orderState: "1",
        externalCode: "1",
        version: 1,
        remarks: null,
        deleted: 0,
        ids: null,
        sequenceCode: 1,
      },
      {
        id: "1",
        createTime: "2024-05-05 11:03:36",
        lastUpdateTime: null,
        createdUserId: null,
        lastUpdatedUserId: null,
        createdUserName: null,
        lastUpdatedUserName: null,
        produceOrderCode: "1",
        produceOrderType: 1,
        organizationId: "1",
        organizationCode: "100",
        plantId: "1737270157287362562",
        plantCode: "1000",
        workshopId: "1",
        workshopCode: "1",
        workshopSectionId: "1",
        workshopSectionCode: "1",
        workCenterId: "1",
        workCenterCode: "1",
        planDate: "2024-04-22 11:03:17",
        shiftSystemId: "1",
        shiftId: "1",
        routingAllocationId: "1",
        materialId: "1",
        materialCode: "1",
        planQuantity: 1.0,
        planBatch: "1",
        planFinishQuantity: 2.0,
        quantityUnit: "1",
        produceOrderSource: "1",
        producePlanId: "1",
        producePlanCode: "1",
        parentProduceOrderId: "1",
        orderState: "1",
        externalCode: "1",
        version: 1,
        remarks: null,
        deleted: 0,
        ids: null,
        sequenceCode: 1,
      },
      {
        id: "1",
        createTime: "2024-05-06 11:03:36",
        lastUpdateTime: null,
        createdUserId: null,
        lastUpdatedUserId: null,
        createdUserName: null,
        lastUpdatedUserName: null,
        produceOrderCode: "1",
        produceOrderType: 1,
        organizationId: "1",
        organizationCode: "100",
        plantId: "1737270157287362562",
        plantCode: "1000",
        workshopId: "1",
        workshopCode: "1",
        workshopSectionId: "1",
        workshopSectionCode: "1",
        workCenterId: "1",
        workCenterCode: "1",
        planDate: "2024-04-23 11:03:17",
        shiftSystemId: "1",
        shiftId: "1",
        routingAllocationId: "1",
        materialId: "1",
        materialCode: "1",
        planQuantity: 3.0,
        planBatch: "1",
        planFinishQuantity: 1.0,
        quantityUnit: "1",
        produceOrderSource: "1",
        producePlanId: "1",
        producePlanCode: "1",
        parentProduceOrderId: "1",
        orderState: "1",
        externalCode: "1",
        version: 1,
        remarks: null,
        deleted: 0,
        ids: null,
        sequenceCode: 0,
      },
    ],
    requestPlanList: [
      {
        id: "1",
        createTime: null,
        lastUpdateTime: null,
        createdUserId: null,
        lastUpdatedUserId: null,
        createdUserName: null,
        lastUpdatedUserName: null,
        organizationId: null,
        organizationCode: null,
        plantId: null,
        plantCode: null,
        workCenterId: null,
        workCenterCode: null,
        producePlanId: "1",
        producePlanCode: null,
        produceOrderId: null,
        produceOrderCode: null,
        planDate: null,
        planYear: null,
        planWeek: null,
        shiftSystemId: null,
        shiftId: null,
        materialId: "1111",
        materialCode: null,
        materialBarcode: null,
        sequenceCode: null,
        isRelease: null,
        isExecute: null,
        requestQuantity: null,
        finishQuantity: null,
        version: null,
        remarks: null,
        deleted: 0,
        ids: null,
      },
      {
        id: "2",
        createTime: null,
        lastUpdateTime: null,
        createdUserId: null,
        lastUpdatedUserId: null,
        createdUserName: null,
        lastUpdatedUserName: null,
        organizationId: null,
        organizationCode: null,
        plantId: null,
        plantCode: null,
        workCenterId: null,
        workCenterCode: null,
        producePlanId: "1",
        producePlanCode: null,
        produceOrderId: null,
        produceOrderCode: null,
        planDate: null,
        planYear: null,
        planWeek: null,
        shiftSystemId: null,
        shiftId: null,
        materialId: "1111",
        materialCode: null,
        materialBarcode: null,
        sequenceCode: null,
        isRelease: null,
        isExecute: null,
        requestQuantity: null,
        finishQuantity: null,
        version: null,
        remarks: null,
        deleted: 0,
        ids: null,
      },
    ],
  },
  {
    id: "2",
    createTime: null,
    lastUpdateTime: null,
    createdUserId: null,
    lastUpdatedUserId: null,
    createdUserName: null,
    lastUpdatedUserName: null,
    producePlanCode: null,
    routingAllocationId: null,
    organizationId: null,
    organizationCode: null,
    plantId: "11",
    plantCode: null,
    workshopId: null,
    workshopCode: null,
    workshopSectionId: null,
    workshopSectionCode: null,
    workCenterId: "22",
    workCenterCode: null,
    planBeginDate: null,
    planEndDate: null,
    planYear: 2024,
    planWeek: 17,
    materialId: "1111",
    materialCode: "1111code",
    planState: 0,
    planQuantity: 1212.0,
    planBatch: "202410",
    planFinishQuantity: null,
    quantityUnit: null,
    externalCode: null,
    version: null,
    remarks: null,
    deleted: 0,
    ids: null,
    yield: "3000",
    orderList: [
      {
        id: "1",
        createTime: "2024-05-06 11:03:36",
        lastUpdateTime: null,
        createdUserId: null,
        lastUpdatedUserId: null,
        createdUserName: null,
        lastUpdatedUserName: null,
        produceOrderCode: "1",
        produceOrderType: 1,
        organizationId: "1",
        organizationCode: "100",
        plantId: "1737270157287362562",
        plantCode: "1000",
        workshopId: "1",
        workshopCode: "1",
        workshopSectionId: "1",
        workshopSectionCode: "1",
        workCenterId: "1",
        workCenterCode: "1",
        planDate: "2024-04-22 11:03:17",
        shiftSystemId: "1",
        shiftId: "1",
        routingAllocationId: "1",
        materialId: "1",
        materialCode: "1",
        planQuantity: 1.0,
        planBatch: "1",
        planFinishQuantity: 2.0,
        quantityUnit: "1",
        produceOrderSource: "1",
        producePlanId: "1",
        producePlanCode: "1",
        parentProduceOrderId: "1",
        orderState: "1",
        externalCode: "1",
        version: 1,
        remarks: null,
        deleted: 0,
        ids: null,
        sequenceCode: 1,
      },
      {
        id: "1",
        createTime: "2024-05-05 11:03:36",
        lastUpdateTime: null,
        createdUserId: null,
        lastUpdatedUserId: null,
        createdUserName: null,
        lastUpdatedUserName: null,
        produceOrderCode: "1",
        produceOrderType: 1,
        organizationId: "1",
        organizationCode: "100",
        plantId: "1737270157287362562",
        plantCode: "1000",
        workshopId: "1",
        workshopCode: "1",
        workshopSectionId: "1",
        workshopSectionCode: "1",
        workCenterId: "1",
        workCenterCode: "1",
        planDate: "2024-04-22 11:03:17",
        shiftSystemId: "1",
        shiftId: "1",
        routingAllocationId: "1",
        materialId: "1",
        materialCode: "1",
        planQuantity: 1.0,
        planBatch: "1",
        planFinishQuantity: 2.0,
        quantityUnit: "1",
        produceOrderSource: "1",
        producePlanId: "1",
        producePlanCode: "1",
        parentProduceOrderId: "1",
        orderState: "1",
        externalCode: "1",
        version: 1,
        remarks: null,
        deleted: 0,
        ids: null,
        sequenceCode: 1,
      },
      {
        id: "1",
        createTime: "2024-05-06 11:03:36",
        lastUpdateTime: null,
        createdUserId: null,
        lastUpdatedUserId: null,
        createdUserName: null,
        lastUpdatedUserName: null,
        produceOrderCode: "1",
        produceOrderType: 1,
        organizationId: "1",
        organizationCode: "100",
        plantId: "1737270157287362562",
        plantCode: "1000",
        workshopId: "1",
        workshopCode: "1",
        workshopSectionId: "1",
        workshopSectionCode: "1",
        workCenterId: "1",
        workCenterCode: "1",
        planDate: "2024-04-23 11:03:17",
        shiftSystemId: "1",
        shiftId: "1",
        routingAllocationId: "1",
        materialId: "1",
        materialCode: "1",
        planQuantity: 3.0,
        planBatch: "1",
        planFinishQuantity: 1.0,
        quantityUnit: "1",
        produceOrderSource: "1",
        producePlanId: "1",
        producePlanCode: "1",
        parentProduceOrderId: "1",
        orderState: "1",
        externalCode: "1",
        version: 1,
        remarks: null,
        deleted: 0,
        ids: null,
        sequenceCode: 0,
      },
    ],
    requestPlanList: [
      {
        id: "1",
        createTime: null,
        lastUpdateTime: null,
        createdUserId: null,
        lastUpdatedUserId: null,
        createdUserName: null,
        lastUpdatedUserName: null,
        organizationId: null,
        organizationCode: null,
        plantId: null,
        plantCode: null,
        workCenterId: null,
        workCenterCode: null,
        producePlanId: "1",
        producePlanCode: null,
        produceOrderId: null,
        produceOrderCode: null,
        planDate: null,
        planYear: null,
        planWeek: null,
        shiftSystemId: null,
        shiftId: null,
        materialId: "1111",
        materialCode: null,
        materialBarcode: null,
        sequenceCode: null,
        isRelease: null,
        isExecute: null,
        requestQuantity: null,
        finishQuantity: null,
        version: null,
        remarks: null,
        deleted: 0,
        ids: null,
      },
    ],
  },
];

export const oneTable = [
  {
    id: "",
    plantId: "",
    plantCode: "",
    materialId: "", // 物料id
    materialCode: "", // 物料编号
    version: "", // 版本
    versionNo: "", // 版本号
    planState: 0, // 生产状态
    yield: "", // 班产
    planQuantity: "", // 计划数量
    planBatch: "", // 计划批次
    workCenterCode: "", //工作中心code
    workCenterId: "", //工作中心id
    planYear: "", //工作年
    planWeek: "", //工作周
    planFinishQuantity: "", //已完成数量
    routingAllocationId: "", // 工艺路线分配物料id
    orderList: [], // 生产计划信息
    requestPlanList: [], // 卷料信息
  },
];

const planStateList = [
  {
    name: "新建",
    value: 0,
  },
  {
    name: "审核",
    value: 1,
  },
  {
    name: "下达",
    value: 2,
  },
  {
    name: "生产",
    value: 3,
  },
  {
    name: "完成",
    value: 4,
  },
];

const oneLine = [
  {
    id: "",
    materialId: "materialId", // 物料id
    materialCode: "materialCode", // 物料编号
    planState: "", // 生产状态
    yield: "", // 班产
    planQuantity: "", // 计划数量
    planBatch: "", // 计划批次
    unPlanQuantity: "", // 未排数量？？？
    workCenterCode: "", //工作中心code
    workCenterId: "", //工作中心id
    planYear: "", //工作年
    planWeek: "", //工作周
    planFinishQuantity: "", //已完成数量
  },
];
